package com.aiyige.page.publish.learnvideo.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.publish.learnvideo.model.PublishLearnVideoItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.ExcludeEmojiInputFilter;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhuyakun on 2017/11/8.
 */

public class PublishLearnVideoAdapter extends BaseQuickAdapter<PublishLearnVideoItem, PublishLearnVideoAdapter.ViewHolder> {

    ItemTouchHelper itemTouchHelper;
    Callback callback = null;

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public PublishLearnVideoAdapter() {
        super(R.layout.publish_learn_video_item, new LinkedList<PublishLearnVideoItem>());
        itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                int swipeFlags = 0;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                Collections.swap(getData(), viewHolder.getAdapterPosition(), target.getAdapterPosition());
                notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                notifyDataSetChanged();
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }

            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }
        });
    }

    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    protected void convert(ViewHolder helper, PublishLearnVideoItem item) {
        helper.bindData(item);
    }

    public interface Callback {
        void inputTitle(String title);
    }

    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.orderTv)
        TextView orderTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.mediaTitleEt)
        EditText mediaTitleEt;
        @BindView(R.id.durationTv)
        TextView durationTv;
        @BindView(R.id.actionLayout)
        FrameLayout actionLayout;
        @BindView(R.id.trailBtn)
        TextView trailBtn;
        @BindView(R.id.previewBtn)
        TextView previewBtn;
        @BindView(R.id.editCoverTv)
        TextView editCoverTv;
        PublishLearnVideoItem data;


        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.coverLayout);
            addOnClickListener(R.id.actionLayout);
            addOnClickListener(R.id.trailBtn);
            addOnClickListener(R.id.previewBtn);
            mediaTitleEt.setFilters(new InputFilter[]{new ExcludeEmojiInputFilter(), new InputNumFilter(PublishUtil.MAX_VIDEO_TITLE_LENGTH, false)});
            mediaTitleEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    data.setTitle(s.toString().trim());
                    if (callback != null) {
                        callback.inputTitle(data.getTitle());
                    }
                }
            });
            mediaTitleEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
//                        if (TextUtils.isEmpty(data.getTitle())) {
//                            data.setTitle(String.format(StringUtils.getString(R.string.episode_no), getAdapterPosition() + 1));
//                            mediaTitleEt.setText(data.getTitle());
//                        }
                    } else {
                        mediaTitleEt.setSelection(mediaTitleEt.getText().length());
                    }
                }
            });

        }

        public void bindData(PublishLearnVideoItem item) {
            data = item;
            if (data.isPreUpload()) {
                editCoverTv.setVisibility(View.INVISIBLE);
            } else {
                editCoverTv.setVisibility(View.VISIBLE);
            }
            orderTv.setText(String.valueOf(getAdapterPosition() + 1));
            Glide.with(itemView.getContext())
                    .load(data.getCoverUrl())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            mediaTitleEt.setText(data.getTitle());
            durationTv.setText(StringUtils.formatMediaDurationMS2(data.getDuration()));
            trailBtn.setSelected(data.isTrail());
        }
    }

}
